package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcConsultMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.constant.LongRangeServiceOrderStatusConstant;
import com.csun.cmny.provider.model.domain.OmcConsult;
import com.csun.cmny.provider.model.domain.OmcLongRangeServiceOrder;
import com.csun.cmny.provider.model.domain.OmcSpecialist;
import com.csun.cmny.provider.model.dto.consult.ConsultDisposeDto;
import com.csun.cmny.provider.model.dto.consult.ConsultSaveDto;
import com.csun.cmny.provider.model.dto.consult.ConsultSelectDto;
import com.csun.cmny.provider.model.vo.consult.ConsultVo;
import com.csun.cmny.provider.service.OmcConsultService;
import com.csun.cmny.provider.service.OmcLongRangeServiceOrderService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class OmcConsultServiceImpl extends BaseService<OmcConsult> implements OmcConsultService {
    @Resource
    private OmcConsultMapper consultMapper;

    @Resource
    private OmcLongRangeServiceOrderService longRangeServiceOrderService;



    @Override
    public List<ConsultVo> selectConsult(ConsultSelectDto consultSelectDto) {
        return consultMapper.selectConsult(consultSelectDto);
    }

    @Override
    public int saveconsult(ConsultSaveDto consultSaveDto, LoginAuthDto loginAuthDto) {

        //校验订单是否存在
        OmcLongRangeServiceOrder omcLongRangeServiceOrder = longRangeServiceOrderService.selectByKey(consultSaveDto.getLongRangeServiceOrderId());;
        if (PublicUtil.isEmpty(omcLongRangeServiceOrder)){
            throw new OmcBizException(ErrorCodeEnum.OMC10080038);
        }
        //校验订单是否是已下单待咨询状态
        if (omcLongRangeServiceOrder.getOrderStatus()!=LongRangeServiceOrderStatusConstant.PLACEDANORDER){
            throw new OmcBizException(ErrorCodeEnum.OMC10080039);
        }


        //新增咨询信息
        OmcConsult omcConsult = new OmcConsult();
        BeanUtils.copyProperties(consultSaveDto,omcConsult);
        omcConsult.setSpecialistId(omcLongRangeServiceOrder.getSpecialistId());
        omcConsult.setCreatorId(loginAuthDto.getUserId());
        omcConsult.setCreator(loginAuthDto.getUserName());
        omcConsult.setGroupId(loginAuthDto.getGroupId());
        int save = save(omcConsult);

        //变更订单状态

        omcLongRangeServiceOrder.setOrderStatus(LongRangeServiceOrderStatusConstant.WAITFORREPLY);
        longRangeServiceOrderService.update(omcLongRangeServiceOrder);

        return save;
    }

    @Override
    public int dispose(ConsultDisposeDto consultDisposeDto, OmcLongRangeServiceOrder omcLongRangeServiceOrder) {

        //回复信息
        OmcConsult omcConsult = new OmcConsult();
        BeanUtils.copyProperties(consultDisposeDto,omcConsult);
        omcConsult.setReplyTime(new Date());
        int update = update(omcConsult);

        //修改订单状态
        OmcLongRangeServiceOrder serviceOrder = new OmcLongRangeServiceOrder();
        serviceOrder.setId(omcLongRangeServiceOrder.getId());
        serviceOrder.setOrderStatus(LongRangeServiceOrderStatusConstant.ANSWERED);
        longRangeServiceOrderService.update(serviceOrder);
        return update;
    }


}
